
** Clean adresses and drop duplicate adresses
use "$root/Data/Original/bfs_merge_2019.dta", clear


do "$root/Code/Management/Merge_Prepare_Adress_01.do"
duplicates tag EGID, g(duplicates)
drop if duplicates>0
drop duplicates
duplicates drop Adress_withletter, force
save "$root/Data/Original/bfs_merge_clean_2019.dta", replace


* Merge with Tax Data
use "$root/Data/Original/Grunddaten_merge.dta", clear
merge m:1 Adress_withletter using "$root/Data/Original/bfs_merge_clean_2019.dta"

keep if _merge==3
drop _
drop Name 
duplicates drop PID year, force

* Save matched sample
save "$root/Data/Original/bfs_sample_2019.dta", replace

* Save PID EGID correspondence
keep year PID EGID
save "$root/Data/Original/EGID_PID_2019.dta", replace
